<template>
  <div>
    <!-- 面包屑导航 -->
    <div class="center-w">
      <div class="head">
        <div class="title">控制管理</div>
      </div>
      <el-table :data="tableData">
        <el-table-column type="index" align="center"> </el-table-column>
        <el-table-column prop="name" align="center" label="控制显示隐藏的类别">
        </el-table-column>
        <el-table-column prop="title" align="center" label="备注">
          <template slot-scope="scope">
            <div style="display: flex; align-items: center;">
              <el-input
                type="textarea"
                autosize
                maxlength="150"
                show-word-limit
                placeholder="请输入"
                v-model="scope.row.remark"
                :disabled="scope.row.name == 'WeChatOpen'"
                clearable
              ></el-input>
              <div style="margin: 0 10px;"></div>
              <el-button
                :disabled="scope.row.name == 'WeChatOpen'"
                @click="submitRemark(scope.row.id, scope.row.remark)"
                type="success"
                size="mini"
                >提交</el-button
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-switch
              @change="updateState(scope.row)"
              v-model="scope.row.isOpen"
            ></el-switch>
            <span style="margin-left: 10px;">{{
              scope.row.isOpen ? "开" : "关"
            }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { updateWeChatOpen, weChatOpenList } from "../../api/set";

export default {
  data() {
    return {
      // 目前没有做列表处理 后续需要多功能控制在进行优化处理
      tableData: []
    };
  },

  created() {
    this._weChatOpenList();
  },
  methods: {
    //获取当获取配置列表
    async _weChatOpenList() {
      let result = await weChatOpenList();
      if (result.code == 200) {
        this.tableData = result.data;
        // this.tableData.forEach(e => {
        //   if (e.name == "BroadcastOpen") {
        //     e.name = "BroadcastOpen--小程序广播控制";
        //   }
        // });
      } else {
        this.$message.error(res.message);
      }
    },

    // 编辑input里面的值
    submitRemark(id, y) {
      let data = {
        id,
        remark: y
      };
      updateWeChatOpen(data).then(res => {
        if (res.code == 200) {
          this.$message.success(`修改成功`);
          this._weChatOpenList();
        } else {
          this.$message.error(res.message);
        }
      });
    },

    //开关控制
    updateState(r) {
      this.$confirm(`确认${r ? "打开" : "关闭"}吗? `, "确认信息", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(() => {
          updateWeChatOpen({
            isOpen: r.isOpen,
            id: r.id
          }).then(res => {
            if (res.code == 200) {
              this.$message.success(`已${r ? "打开" : "关闭"}`);
              this._weChatOpenList();
            }
          });
        })
        .catch(action => {
          this.$message.info("取消操作");
          this._weChatOpenList();
        });
    }
  }
};
</script>

<style lang="less" scoped>
.center-w {
  display: flex;
  /* align-items: center; */
  flex-direction: column;
  background-color: #fff;
  height: 85vh;
  padding: 20px;
  border: 1px solid #ccc;
  margin: 20px;
}

.title {
  color: rgba(16, 16, 16, 1);
  font-size: 20px;
  margin-bottom: 10px;
}

/deep/ .el-table .cell {
  font-weight: 700 !important;
  margin-left: 30px;
}

.head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
